package com.xysq.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CustomerBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.InjectView;

public class GetBackPasswordActivity extends BaseActivity {

    @InjectView(R.id.edt_login_phone)
    EditText loginPhoneEdt;
    @InjectView(R.id.edt_login_password)
    EditText loginPasswordEdt;
    @InjectView(R.id.edt_code)
    EditText codeEdt;
    @InjectView(R.id.btn_get_identifying_code)
    Button getIndentifyingCodeBtn;

    private int getCodeTime;
    private Timer mTimer;
    private TimeTask mTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_back_password);
        mTimer = new Timer();
    }

    public void toBack(View view) {
        onBackPressed();
    }

    public void toSendSmsCode(View view) {
        final String phoneNumber = loginPhoneEdt.getText().toString();
        if (TextUtils.isEmpty(phoneNumber)) {
            ToastUtil.showShort(this, "请输入手机号");
            return;
        }
        if (phoneNumber.length() != 11) {
            ToastUtil.showShort(this, "请输入有效的手机号");
            return;
        }
        appAction.checkCustExist(phoneNumber, new CallbackListener<CustomerBO>() {
            @Override
            public void onSuccess(CustomerBO data) {
                if (data != null) {
                    sendSMS(phoneNumber);
                } else {
                    ToastUtil.showShort(getBaseContext(), "手机号码未注册");
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(getBaseContext(), message);
            }
        });

    }

    private void sendSMS(String phoneNumber) {
        appAction.sendSmsCode4ResetPassword(phoneNumber, new CallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showShort(getBaseContext(), getResources().getString(R.string.toast_send_sms_success));
                getCodeTime = 60;
                mTask = new TimeTask();
                mTimer.schedule(mTask, 0, 1000);
                getIndentifyingCodeBtn.setEnabled(false);
            }

            @Override
            public void onFailure(String errorEvent, String message) {
                ToastUtil.showShort(getBaseContext(), message);
            }
        });
    }

    public void toResetPassword(View view) {
        final String phoneNumber = loginPhoneEdt.getText().toString();
        final String password = loginPasswordEdt.getText().toString();
        final String code = codeEdt.getText().toString();
        if (TextUtils.isEmpty(phoneNumber)) {
            ToastUtil.showShort(this, "请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showShort(this, "请输入密码");
            return;
        }
        if (password.length() < 6 || password.length() > 15) {
            ToastUtil.showShort(this, "请输入6-15位密码");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showShort(this, "请输入验证码");
            return;
        }
        appAction.checkCustExist(phoneNumber, new CallbackListener<CustomerBO>() {
            @Override
            public void onSuccess(CustomerBO data) {
                if (data != null) {
                    appAction.resetLoginPassword(phoneNumber, password, code, new CallbackListener<Void>() {
                        @Override
                        public void onSuccess(Void data) {
                            ToastUtil.showShort(getBaseContext(), "密码重置成功");
                            finish();
                        }

                        @Override
                        public void onFailure(String errorCode, String message) {
                            ToastUtil.showShort(getBaseContext(), message);
                        }
                    });
                } else {
                    ToastUtil.showShort(getBaseContext(), "手机号码未注册");
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(getBaseContext(), message);
            }
        });
    }

    class TimeTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (getCodeTime < 1) {
                        getIndentifyingCodeBtn.setEnabled(true);
                        getIndentifyingCodeBtn.setText(getResources().getString(R.string.txt_get_code));
                        if (mTask != null) {
                            mTask.cancel();
                        }
                    } else {
                        getIndentifyingCodeBtn.setText("" + getCodeTime);
                    }
                    getCodeTime--;
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("忘记密码页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("忘记密码页");
        MobclickAgent.onPause(this);
    }
}
